<table width="100%" border="0" cellspacing="0" cellpadding="0">
<?php
$_rowHeader = "THỐNG KÊ CHUNG";
include "views/RowHeader.php";

$db = new HocKy(true);
$db->setQuery("SELECT XL_TEN FROM XEP_LOAI_DIEM_REN_LUYEN ORDER BY XL_DIEM_SAN DESC");
$result = $db->fetchAll();
$xepLoai = array();
while ($row = mysql_fetch_assoc($result))
	$xepLoai[] = $row['XL_TEN'];

?>
    <tr>
        <td colspan="3">
            <table width="100%" border="1" cellspacing="0" cellpadding="2" class="table_content">
                <tr>
                    <th rowspan="2" width="5%">STT</th>
                    <th rowspan="2" width="35%">Tên lớp</th>
                    <th colspan="<?php echo count($xepLoai); ?>">Xếp loại</th>
                    <th rowspan="2" width="7%">Chưa chấm</th>
                </tr>
                <tr>
				<?php
					foreach ($xepLoai as $ten)
						echo '<th>' . $ten . '</th>';
				?>
                </tr>
                <?php
				
				$danhSachLop = $user->layDanhSachLop(true);
				
				if (count($danhSachLop)) {
					$i = 1;
					foreach ($danhSachLop as $lop) {
						$db->setQuery('SELECT COUNT(SV_MA_SO) AS SO_LUONG_SV FROM SINH_VIEN '
									   . 'WHERE LOP_MA_SO = \'' . $lop['LOP_MA_SO']. '\'');
						$row = mysql_fetch_assoc($db->fetchAll());
						$soLuongSV = $row['SO_LUONG_SV'];
						$soLuongDaChamDiem = 0;

				?>
                <tr>
                    <th><?php echo $i++; ?></th>
                    <td><?php echo $lop['LOP_TEN'] . ' - K' . $lop['KH_MA_SO']; ?></td>
                    <?php
                    if ($soLuongSV) {			
						$db->setQuery('SELECT x.XL_TEN, COUNT(s.SV_MA_SO) AS SO_LUONG '
									 . 'FROM SINH_VIEN s, CO_DIEM_REN_LUYEN c, XEP_LOAI_DIEM_REN_LUYEN x '
									 . 'WHERE s.LOP_MA_SO = \'' . $lop['LOP_MA_SO'] . '\' '
									 . 'AND c.HK_MA_SO = \'' . $db->getMaHocKy() . '\' '
									 . 'AND s.SV_MA_SO = c.SV_MA_SO AND '
									 . '(c.TONG_DIEM_HDK BETWEEN x.XL_DIEM_SAN AND x.XL_DIEM_TRAN) '
									 . 'GROUP BY x.XL_TEN ORDER BY x.XL_DIEM_SAN DESC');
						$result = $db->fetchAll();
						
						$loaiIndex = 0;
						while ($row = mysql_fetch_assoc($result)) {
							while ($xepLoai[$loaiIndex++] != $row['XL_TEN']) {
								echo '<td align="center"> - </td>';
							}
							
							echo '<td align="center">' . $row['SO_LUONG'] / $soLuongSV * 100 . '%</td>';
							$soLuongDaChamDiem += $row['SO_LUONG'];
						}
						
						$loaiIndex = count($xepLoai) - $loaiIndex;
						while ($loaiIndex--) echo '<td align="center"> - </td>';
						
						echo '<td align="center">' . ($soLuongSV - $soLuongDaChamDiem) / $soLuongSV * 100 . '%</td>';
					} else {
						echo '<td colspan="' . (count($xepLoai) + 1) . '" align="center">Lớp này không có sinh viên nào.</td>';
					} ?>
                </tr>
				<?php
					}
				} else {
					echo '<tr><td colspan="' . (count($xepLoai) + 3) . '" align="center">Không có lớp nào</td></tr>';
				}
				?>
        </table></td>
    </tr>
</table>
